<?php

namespace app\biz\controller;

use app\base\BaseController;
use app\biz\model\Baidusn;
use app\biz\model\Content;
use think\Console;
use think\facade\Cache;
use utils\LogUtils;

class Biz extends BaseController
{
    public function startServer()
    {

        $output = Console::call('webSocket', ['name' => '1']);
        return $output->fetch();
    }

    public function stopServer()
    {

        $output = Console::call('webSocket', ['name' => '0']);
        return $output->fetch();
    }

    public function sendData()
    {

        $data = new \MyConfig();
        $json2 = ['data' => [$data]];
        echo postRequestJson('http://www.tn.cc/index.php/biz/biz/setNumData', $json2, false, false);

    }

    /**
     * http://www.tn.cc/index.php/biz/biz/setNumData
     * http://www.tn.cc/Biz/Biz/setNumData
     * {"code":200,"message":"success","data":[{"0":"231","id":"231","1":"999","From":"510185001","2":"1","Win_ID":"1","3":"A002","call_no":"A002","4":"\u9177\u6613","Call_Name":"\u9177\u6613"}],"location":""}
     *  {"data":{"From":"510185001","Win_ID":"20","call_no":"A007","Call_Name":""}}
     */
    public function setNumData()
    {
        $param = input();
        //LogUtils::log($param);
        try {
            if (!isset($param['data'])) {
                return $this->fail02("data为空");
            }
            if (!isset($param['data']['From']) || !isset($param['data']['Call_NO'])) {
                return $this->fail02("From/call_no为空");
            }
        } catch (\Exception $e) {
            return $this->fail02("数据格式不正确");
        }

        //$Device_ID = $param['data'][0]['From'];

        //$num_data_array = Cache::get("num_data", []);

        //array_push($num_data_array, $param);

        //Cache::set("num_data", $num_data_array);

        //$su = json_encode($param, JSON_UNESCAPED_UNICODE);
        //LogUtils::log($su);
        //echo $su;
        $param['data']['mytime'] = time();
        $su = json_encode($param, JSON_UNESCAPED_UNICODE);
        LogUtils::log($su);
        Content::create(['tab' => 1, "content" => $su]);
         $this->ok();
    }

    public function setPrintData()
    {
        $param = input();
        //LogUtils::log($param);
        try {
            if (!isset($param['data'])) {
                return $this->fail02("data为空");
            }
            if (!isset($param['data']['From'])) {
                return $this->fail02("From为空");
            }
        } catch (\Exception $e) {
            return $this->fail02("数据格式不正确");
        }

        //$Device_ID = $param['data'][0]['From'];

        //$print_data_array = Cache::get("print_data", []);

        //array_push($print_data_array, $param);

        //Cache::set("print_data", $print_data_array);

        //$su = json_encode($param, JSON_UNESCAPED_UNICODE);
        //LogUtils::log($su);
        //echo $su;
        $param['data']['mytime'] = time();
        $su = json_encode($param, JSON_UNESCAPED_UNICODE);
        LogUtils::log($su);
        Content::create(['tab' => 2, "content" => $su]);
        $this->ok();
    }

    public function setChatData()
    {
        $param = input();
        //LogUtils::log('setChatData');
        /*$param = array (
            'data' =>
                array (
                    'From' => '999',
                    'Websocket' => '1957222',
                    'nickname' => 'jingfeng',
                    'headimgurl' => 'https://thirdwx.qlogo.cn/mmopen/vi_32/6rZGK7527tK8lk5L3rjeG5pu3iaiczCNYoMfoUu6qbEmf9QxnINVrCNYN27rQFlwQ2qXe2sZcDFuTMZBroUuhbAw/132',
                    'adddate' => '2023-12-26 16:20:20',
                    'info' => '可以',
                    'img' => '',
                    'orderid' => NULL,
                    'member' => '1957222',
                    'memberid' => '1957222',
                ),
        );*/
        LogUtils::log($param);
        try {
            if (!isset($param['data'])) {
                return $this->fail02("data为空");
            }
            if (!isset($param['data']['From'])) {
                return $this->fail02("From为空");
            }
        } catch (\Exception $e) {
            return $this->fail02("数据格式不正确");
        }

        //$su = json_encode($param, JSON_UNESCAPED_UNICODE);
        //LogUtils::log($su);

        //$Device_ID = $param['data'][0]['From'];

        //$data_array = Cache::get("chat_data", []);

        //array_push($data_array, $param);
       //Cache::set("chat_data", $data_array);

        $param['data']['mytime'] = time();
        $su = json_encode($param, JSON_UNESCAPED_UNICODE);
        LogUtils::log($su);
        Content::create(['tab' => 3, "content" => $su]);
        $this->ok();
    }

    /**
     * @return void
     *{
     * "data": {
     * "siteid": "999",
     *  "member_id": "1957388",
     *  "tpl_id": "2",
     *  "url": "http://wx.kyd5.cn/paiduiapi/task.php"
     *  }
     * }
     *
     */
    public function setQunfaMessage()
    {

        //$p = input('data');
        //LogUtils::log($p);
        //$array = ['url'=>$p['url'],'siteid'=>$p['siteid'],'member_id'=>$p['member_id'],'tpl_id'=>$p['tpl_id']];
        //array_push($data_array, $p);
        //$data_array = Cache::get("qunfa_data", []);
        //Cache::set("qunfa_data", $data_array);
        //修改了数据存储逻辑
        $param = input();
        $param['data']['mytime'] = time();
        $su = json_encode($param, JSON_UNESCAPED_UNICODE);
        LogUtils::log($su);
        Content::create(['tab' => 4, "content" => $su]);
        $this->ok();
    }



    /**
     * http://www.mysocket.cc/index.php/biz/biz/setAppData
     * http://www.mysocket.cc/index.php/biz/biz/setAppData
     *  {"data":{"From":"510185001","Websocket":"20","Data":"A007"}}
     */
    public function setAppData()
    {
        $param = input();
        //LogUtils::log(json_encode($param, JSON_UNESCAPED_UNICODE));
        try {
            if (!isset($param['data'])) {
                return $this->fail02("data为空");
            }
            if (!isset($param['data']['From'])) {
                return $this->fail02("From");
            }
        } catch (\Exception $e) {
            return $this->fail02("数据格式不正确");
        }

        //$Device_ID = $param['data'][0]['From'];

        //$array = Cache::get("app_data", []);

        //array_push($array, $param);

        //Cache::set("app_data", $array);

        //$this->ok($param);
        $param['data']['mytime'] = time();
        $su = json_encode($param, JSON_UNESCAPED_UNICODE);
        LogUtils::log($su);
        Content::create(['tab' => 5, "content" => $su]);
        $this->ok();
    }


    public function rquestsn()
    {
        $device = input("device");
        if (empty($device)) {
            $this->fail02("设备错误");
        }

        //把以前他自己的给他
        $bean = Baidusn::where("device", $device)->find();
        if ($bean) {
            $bean01 = new Baidusn();
            $bean01->save([
                'gettime' => date('Y-m-d H:i:s')
            ], ["Id" => $bean->Id]);
            $this->ok($bean->sn);
        }

        //取一个新的返回
        $bean = Baidusn::where("device", "")->find();

        if ($bean) {
            $bean01 = new Baidusn();
            $bean01->save([
                'device'  => $device,
                'gettime' => date('Y-m-d H:i:s')
            ], ["Id" => $bean->Id]);
            $this->ok($bean->sn);
        } else {
            $this->fail02("无多余sn");
        }
    }
}